package cn.edu.hqu.cst.android.ex2;


import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;

public class DialogTest extends Activity {

    private int[] date = new int[100];
    private int hasDate = 0;
    final int progress_Dialog = 0x112;
    private int progressStatus = 0;
    private ProgressDialog pd;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_test);

        Button exec = (Button) this.findViewById(R.id.b);
        exec.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(progress_Dialog);
            }
        });
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 0x112) {
                    pd.setProgress(progressStatus);
                }
            }
        };
        Button back = findViewById(R.id.ba);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent= new Intent(DialogTest.this,MainActivity.class);
                startActivities(new Intent[]{intent});
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id, Bundle args)
    {
        System.out.println("---------------create---------------");
        switch (id)
        {
            case progress_Dialog:
                pd = new ProgressDialog(this);
                pd.setMax(100);
                pd.setTitle("任务完成百分比");
                pd.setMessage("耗时任务完成百分比");
// 设置对话框取消按钮不可用
                pd.setCancelable(false);
// 设置对话框进度条样式风格
                pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// 设置对话框的进度条是否显示进度
                pd.setIndeterminate(false);
                break;
        }
        return pd;
    }

    // 该方法将在onCreateDialog()调用之后被回调
    @Override
    protected void onPrepareDialog(int id, Dialog dialog) {
        System.out.println("-----------------prepare-----------------");
        super.onPrepareDialog(id, dialog);
        switch (id) {
            case progress_Dialog:
                pd.incrementSecondaryProgressBy(-pd.getProgress());
                new Thread() {
                    public void run() {
                        while (progressStatus < 100) {
                            progressStatus = doWork();
                            Message msg = new Message();
                            msg.what = 0x112;
                            handler.sendMessage(msg);
                        }
                        if (progressStatus >= 100) {
                            pd.dismiss();// 关闭对话框
                        }
                    }
                }.start();
                break;
        }
    }

    public int doWork() {
        date[hasDate++] = (int) (Math.random() * 100);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return hasDate;
    }
}
